package com.cssy.xxkj.logic.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.entity.LogicProductionPlanDO;
import com.cssy.xxkj.logic.entity.LogicProductionPlanItemDO;
import com.cssy.xxkj.logic.service.ILogicProductionDispatchService;
import com.cssy.xxkj.logic.service.ILogicProductionPlanService;
import com.cssy.xxkj.logic.util.OftenMothedUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.json.OutJsonGrid;
/**
 * 生产计划控制器
 * @author hf
 *
 */
@Controller
@RequestMapping("/logic/proplan")
public class ProductionPlanController extends BaseController<LogicProductionPlanDO> {
	
	private static final Logger logger = Logger.getLogger(ProductionPlanController.class);

	private static final String MANAGER = "/xxkj/logic/productionplan/ProPlanManager";// 列表页面
	private static final String EIDT = "/xxkj/logic/productionplan/ProPlanOperation";// 添加、修改、显示页面

	@Resource(name = "logicProductionPlanService")
	private ILogicProductionPlanService logicProductionPlanService;
	//主数据公用集合Service
	@Resource(name="basicUtilService")
	private IBasicUtilService basicUtilService;
	
	@Resource(name = "logicProductionDispatchService")
	private ILogicProductionDispatchService logicProductionDispatchService;

	@Resource(name = "oftenMothedUtil")
	private OftenMothedUtil oftenMothedUtil;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs=this.oftenMothedUtil.getNoFinishConditions(scs, "zd008Billstatus", BillStatusUtil.PROPLAN_BILL_STATUS_FOUR);
		scs.setOrderBy("bizCreatorDate desc");
		SearchResult result = logicProductionPlanService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);

	}

	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
			throws Exception {
		if (OperationFlag.ADD.equals(flag)) {// 新增加时操作
			model.addAttribute("flag", "ADD");
		} else {
			LogicProductionPlanDO plan = logicProductionPlanService.findListById(id);
			model.addAttribute("proplan", plan);
		}
		return EIDT;
	}
	
	@RequestMapping("/griditems")
	public void griditems(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)){
			LogicProductionPlanDO proplan = logicProductionPlanService.findListById(id);
			List<LogicProductionPlanItemDO> soitem = logicProductionPlanService.findPlanItemList(proplan.getId());
			OutJsonGrid out = new OutJsonGrid(soitem);
			out.printJSon(response);
		}
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicProductionPlanDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String itemdata = request.getParameter("itemdata");
			if (StringUtils.isBlank(itemdata)) {
				bodyMap.isFail();
				bodyMap.configWebInfor("产品列表不能为空！");
				return bodyMap.modelMap;
			}
			logicProductionPlanService.planSave(request, curAP);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			logger.warn(e.getMessage(),e);
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields = ids.split(",");
			for (String field : fields) {
				LogicProductionPlanDO ld = logicProductionPlanService.findListById(field);
				logicProductionPlanService.deleteBizRemoved(ld.getId());
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
			
		} catch (Exception e) {
			logger.warn(e.getMessage(),e);
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}
	@RequestMapping("/checkBack")
	public void checkBack(HttpServletRequest request,
			HttpServletResponse response){
		SearchConditions scs = new SearchConditions();
		String id=request.getParameter("id");//生产计划单号
		scs.addCondition("",id);
		//调度的时候不能有单
		List<LogicProductionPlanDO> listLPP=logicProductionDispatchService.findListByExample(scs).getResults();
		Map<String, Object> outMap= new HashMap<String, Object>();
		if(null==listLPP||listLPP.size()!=1||listLPP.isEmpty()){
			outMap.put("msg", "no");
		}else{
			
		}
	}
	
}
